<?php 
require_once("perficature-utils.php");
insertHeader("Admin");
$gMsg = "";

if ( array_key_exists('clearresults', $_GET) ) {
	$command = "delete from $gSummaryTable;";
	doSimpleCommand($command);
 	$gMsg .= "<p>Results table cleared!</p>\n";
}


if ( array_key_exists('backup', $_GET) ) {
	$now = time();

	$command = "CREATE TABLE $gTestsTable$now SELECT * FROM $gTestsTable;";
	doSimpleCommand($command);

	$command = "CREATE TABLE $gSummaryTable$now SELECT * FROM $gSummaryTable;";
	doSimpleCommand($command);

 	$gMsg .= "<p>Databases backed up!</p>\n";
}

if ( array_key_exists('create', $_GET) ) {
	createTables();
 	$gMsg .= "<p>Tables created!</p>\n";
}

if ( array_key_exists('convertdb', $_GET) ) {
	// read all the records from tests and put in testsdev
	$begin = 10020;
	$end   = 10049;
	$query = "select createdate, ip, latency, hostconn, maxconn, parscript, parsheet, parcssjs, cacheexp, cache25k, cacheredir, cacheresredir, prefetch, gzip, du, useragent, confirmed from tests, useragents where tests.useragentid=useragents.useragentid and testid >= $begin and testid < $end;";
	$result = doQuery($query);
	while ($row = mysql_fetch_assoc($result)) {
		$createdate = $row['createdate'];
		$ip = $row['ip'];
		$latency = ( array_key_exists('latency', $row) ? $row['latency'] : NULL );
		$hostconn = ( array_key_exists('hostconn', $row) ? $row['hostconn'] : NULL );
		$maxconn = ( array_key_exists('maxconn', $row) ? $row['maxconn'] : NULL );
		$parscript = ( array_key_exists('parscript', $row) ? $row['parscript'] : NULL );
		$parsheet = ( array_key_exists('parsheet', $row) ? $row['parsheet'] : NULL );
		$parcssjs = ( array_key_exists('parcssjs', $row) ? $row['parcssjs'] : NULL );
		$cacheexp = ( array_key_exists('cacheexp', $row) ? $row['cacheexp'] : NULL );
		$cache25k = ( array_key_exists('cache25k', $row) ? $row['cache25k'] : NULL );
		$cacheredir = ( array_key_exists('cacheredir', $row) ? $row['cacheredir'] : NULL );
		$cacheresredir = ( array_key_exists('cacheresredir', $row) ? $row['cacheresredir'] : NULL );
		$prefetch = ( array_key_exists('prefetch', $row) ? $row['prefetch'] : NULL );
		$gzip = ( array_key_exists('gzip', $row) ? $row['gzip'] : NULL );
		$du = ( array_key_exists('du', $row) ? $row['du'] : NULL );
		$useragent = $row['useragent'];
		$confirmed = ( array_key_exists('confirmed', $row) ? $row['confirmed'] : NULL );
		$pbrowser = parseUseragent($useragent, $browser, $v1, $v2, $v3);

		$command = "insert into testsdev set " .
			"createdate=$createdate" .
			", ip='$ip'" .
			( NULL === $latency ? "" : ", latency=$latency" ) .
			( NULL === $hostconn ? "" : ", hostconn=$hostconn" ) .
			( NULL === $maxconn ? "" : ", maxconn=$maxconn" ) .
			( NULL === $parscript ? "" : ", parscript=$parscript" ) .
			( NULL === $parsheet ? "" : ", parsheet=$parsheet" ) .
			( NULL === $parcssjs ? "" : ", parcssjs=$parcssjs" ) .
			( NULL === $cacheexp ? "" : ", cacheexp=$cacheexp" ) .
			( NULL === $cache25k ? "" : ", cache25k=$cache25k" ) .
			( NULL === $cacheredir ? "" : ", cacheredir=$cacheredir" ) .
			( NULL === $cacheresredir ? "" : ", cacheresredir=$cacheresredir" ) .
			( NULL === $prefetch ? "" : ", prefetch=$prefetch" ) .
			( NULL === $gzip ? "" : ", gzip=$gzip" ) .
			( NULL === $du ? "" : ", du=$du" ) .
			", useragent='$useragent'" .
			( NULL === $confirmed ? "" : ", confirmed=$confirmed" ) .
			", pbrowser='$pbrowser'" .
			", browser='$browser'" .
			( NULL === $v1 ? "" : ", v1=$v1" ) .
			( NULL === $v2 ? "" : ", v2=$v2" ) .
			( NULL === $v3 ? "" : ", v3='$v3'" ) .
			";";
		doSimpleCommand($command);
	}
	mysql_free_result($result);

 	$gMsg .= "<p>Database converted!</p>\n";
}

?>

<?php echo $gAdminLinks ?>

<?php echo $gMsg ?>

<ul>
  <li> <a href="admin.php?clearresults=1">Clear Results Table</a>
  <li> <a href="admin.php?backup=1">Backup Database</a>
  <li> <a href="admin.php?create=1">Create DB</a>
  <li> <a href="admin.php?convertdb=1">Convert DB</a>
</ul>

<?php insertFooter(); ?>
